import 'package:flutter/material.dart';

class HomePageFormViewPage extends StatelessWidget{

  GlobalKey<FormState> loginKey = new GlobalKey<FormState>();
  String userName = '';
  String password = '';
  void login(){
    var loginForm = loginKey.currentState;

    if(loginForm!=null&&loginForm.validate()){
      loginForm.save();
      print('userName='+userName+'password='+password);
    }

  }
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('表单布局'),

        ),
        body: new Column(
          children: <Widget>[
            new Container(
              padding: const EdgeInsets.all((16.0)),
              child: new Form(
                key: loginKey,
                  child: new Column(
                    children: <Widget>[
                      new TextFormField(
                        decoration: new InputDecoration(
                          labelText: '请输入用户名',
                        ),
                        onSaved: (value){
                          userName = value.toString();
                        },
                        onFieldSubmitted: (value){

                        },
                      ),
                      new TextFormField(
                        decoration: new InputDecoration(
                          labelText: '请输入密码',
                        ),
                        onSaved: (value){
                          password = value.toString();
                          print( value.toString());
                        },
                        onFieldSubmitted: (value){

                        },
                        obscureText: true,
                        validator: (value){
                          return value!=null && value.length < 6 ?'密码长度不过':null;
                        },
                      ),
                    ],
                  ),
              ),
            ),
            new SizedBox(
              width: 340.0,
              height: 42.0,
              child: new ElevatedButton(

                  onPressed: login,
                  onLongPress: login,
                  child: new Text(
                    '登录',
                    style: TextStyle(
                      fontSize: 18.0
                    ),
                  ),
              ),
            ),
          ],
        ),
      ),
    );
  }

}